package com.niu.controller;

import com.niu.pojo.MyResult;
import com.niu.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author 鹅鹅鹅
 * @Version 1.0.0
 * @Date 2025/7/16 17:43
 * @Description 测试文件上传功能
 */

@RestController
@RequestMapping("/testUpload")
public class MyUploadTestController {

    @Autowired
    private FileService fileService;

    // 上传文件到本地
    @PostMapping("/uploadToLocal")
    public MyResult<String> uploadToLocal(String name, Integer age, MultipartFile image) throws IOException {
        System.out.println("name: " + name);
        System.out.println("age: " + age);
        String s = fileService.saveFileToLocal(image);
        return s != null || s != "" ? MyResult.ok("文件位置: " + s) : MyResult.error("文件上传失败");
    }

    // 上传文件到云存储
    @PostMapping("/uploadToOss")
    public MyResult<String> uploadToOss(String name, Integer age, MultipartFile image){
        System.out.println("name: " + name);
        System.out.println("age: " + age);
        String s = fileService.saveFileToOss(image);
        return s != null || s != "" ? MyResult.ok("文件位置: " + s) : MyResult.error("文件上传失败");
    }
}
